package cn.ht.jywrwater.module.ht.service.waterpipenetmaintain;

import cn.ht.jywrwater.module.ht.dal.dataobject.watercount.WaterCountDO;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.ht.jywrwater.module.ht.controller.admin.waterpipenetmaintain.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterpipenetmaintain.WaterPipenetMaintainDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.waterpipenetmaintain.WaterPipenetMaintainConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.waterpipenetmaintain.WaterPipenetMaintainMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 管网养护 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterPipenetMaintainServiceImpl implements WaterPipenetMaintainService {

    @Resource
    private WaterPipenetMaintainMapper waterPipenetMaintainMapper;

    @Override
    public Long createWaterPipenetMaintain(WaterPipenetMaintainCreateReqVO createReqVO) {
        // 插入
        WaterPipenetMaintainDO waterPipenetMaintain = WaterPipenetMaintainConvert.INSTANCE.convert(createReqVO);
        waterPipenetMaintainMapper.insert(waterPipenetMaintain);
        // 返回
        return waterPipenetMaintain.getId();
    }

    @Override
    public void updateWaterPipenetMaintain(WaterPipenetMaintainUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterPipenetMaintainExists(updateReqVO.getId());
        // 更新
        WaterPipenetMaintainDO updateObj = WaterPipenetMaintainConvert.INSTANCE.convert(updateReqVO);
        waterPipenetMaintainMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterPipenetMaintain(Long id) {
        // 校验存在
        validateWaterPipenetMaintainExists(id);
        // 删除
        waterPipenetMaintainMapper.deleteById(id);
    }

    private void validateWaterPipenetMaintainExists(Long id) {
        if (waterPipenetMaintainMapper.selectById(id) == null) {
            throw exception(WATER_PIPENET_MAINTAIN_NOT_EXISTS);
        }
    }

    @Override
    public WaterPipenetMaintainDO getWaterPipenetMaintain(Long id) {
        return waterPipenetMaintainMapper.selectById(id);
    }

    @Override
    public List<WaterPipenetMaintainDO> getWaterPipenetMaintainList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterPipenetMaintainMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterPipenetMaintainDO> getWaterPipenetMaintainPage(WaterPipenetMaintainPageReqVO pageReqVO) {
        return waterPipenetMaintainMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterPipenetMaintainDO> getWaterPipenetMaintainList(WaterPipenetMaintainExportReqVO exportReqVO) {
        return waterPipenetMaintainMapper.selectList(exportReqVO);
    }

    @Override
    public WaterPipenetMaintainVO pipenetMaintainDashboard() {
        WaterPipenetMaintainVO result = new WaterPipenetMaintainVO();
        List<WaterPipenetMaintainDO> allMaintainList = waterPipenetMaintainMapper.selectList();
        Map<Integer, Long> pipenetTypeMaintain = allMaintainList.stream().filter(data -> data.getDeleted() == false)
                .collect(Collectors.groupingBy(WaterPipenetMaintainDO::getType,
                        Collectors.counting()));
        // 转化为百分比
        Map<Integer, Double> pipenetTypeRateMaintain = MapUtil.newHashMap();
        for (Integer s : pipenetTypeMaintain.keySet()) {
            pipenetTypeRateMaintain.put(s, pipenetTypeMaintain.get(s).doubleValue()/allMaintainList.size());
        }
        Map<Integer, Long> pipenetCaliberMaintain = allMaintainList.stream().filter(data -> data.getDeleted() == false)
                .collect(Collectors.groupingBy(WaterPipenetMaintainDO::getCaliber,
                        Collectors.counting()));
        Map<Integer, Long> pipenetMaterialMaintain = allMaintainList.stream().filter(data -> data.getDeleted() == false)
                .collect(Collectors.groupingBy(WaterPipenetMaintainDO::getMaterial,
                        Collectors.counting()));
        Map<Integer, Long> pipenetReasonMaintain = allMaintainList.stream().filter(data -> data.getDeleted() == false)
                .collect(Collectors.groupingBy(WaterPipenetMaintainDO::getReason,
                        Collectors.counting()));
        Map<Integer, Long> pipenetSourceMaintain = allMaintainList.stream().filter(data -> data.getDeleted() == false)
                .collect(Collectors.groupingBy(WaterPipenetMaintainDO::getSource,
                        Collectors.counting()));
        result.setPipenetTypeRateMaintain(pipenetTypeRateMaintain);
        result.setPipenetCaliberMaintain(pipenetCaliberMaintain);
        result.setPipenetMaterialMaintain(pipenetMaterialMaintain);
        result.setPipenetReasonMaintain(pipenetReasonMaintain);
        result.setPipenetSourceMaintain(pipenetSourceMaintain);
        return result;
    }

}
